package com.macro.mall.controller;

import com.macro.mall.common.api.CommonResult;
import com.macro.mall.entity.Appraisal;
import com.macro.mall.service.AppraisalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/appraisal")
@Api(tags = "AppraisalController", description = "估价")
public class AppraisalController {

    @Autowired
    private AppraisalService appraisalService;

    @ApiOperation("添加估价记录")
    @RequestMapping(value = "/addOne", method = RequestMethod.POST)
    public CommonResult addOne(@RequestBody Appraisal appraisal) {
        try {
            appraisalService.saveNotNull(appraisal);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
        return CommonResult.success(null, "添加成功");
    }

    @ApiOperation("分页查询记录")
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public CommonResult query(String community, Integer page, Integer size) {
        List<Appraisal> list = null;
        try {
            list = appraisalService.find(community, page, size);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
        return CommonResult.success(list);
    }

    @ApiOperation("删除指导价")
    @RequestMapping(value = "/delOne", method = RequestMethod.POST)
    public CommonResult delOne(Integer id) {
        try {
            appraisalService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
        return CommonResult.success("修改成功");
    }
}
